package com.itany.study.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.study.constant.PaginationConstant;
import com.itany.study.entity.User;
import com.itany.study.exception.LoginException;
import com.itany.study.exception.UsernameExistsException;
import com.itany.study.service.UserService;
import com.itany.study.util.ResponseResult;
import com.itany.study.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2022-11-29 9:59
 * Description：<描述>
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(String username, String password, Model model, HttpSession session){
        try {
            User user = userService.login(username, password);
            session.setAttribute("user",user);
            return "main";
        } catch (LoginException e) {
            model.addAttribute("errorMsg",e.getMessage());
            return "index";
        }
    }

    @ResponseBody
    @RequestMapping("/register")
    public ResponseResult register(User user){
        try {
            userService.register(user);
            return ResponseResult.success();
        } catch (UsernameExistsException e) {
            return ResponseResult.fail(e.getMessage());
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }

    @ResponseBody
    @RequestMapping("/modifyPwd")
    public ResponseResult modifyPwd(String oldpassword, String newpassword, HttpSession session){
        User user = (User) session.getAttribute("user");
        // 判断原密码是否正确
        if (!user.getPassword().equals(oldpassword)){
            return ResponseResult.fail("原密码不正确！");
        }

        // 修改密码
        userService.modifyPwd(user.getId(), newpassword);
        return ResponseResult.success();
    }

    @ResponseBody
    @RequestMapping("/upload")
    public ResponseResult upload(@RequestParam CommonsMultipartFile file, HttpServletRequest req){
        String uploadPath = req.getServletContext().getRealPath("upload");
        String fileName = StringUtil.renameFilename(file.getOriginalFilename());
        String filePath = uploadPath + File.separator + fileName;
        try {
            file.transferTo(new File(filePath));
            return ResponseResult.success(fileName);
        } catch (IOException e) {
            return ResponseResult.fail(e.getMessage());
        }
    }

    @RequestMapping("/list")
    public String list(@RequestParam(required = false, defaultValue = "1") int pageNo, Model model){
        PageHelper.startPage(pageNo, PaginationConstant.pageSize);
        List<User> users = userService.findAll();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        model.addAttribute("pageInfo", pageInfo);
        return "listuser";
    }

}
